﻿/*
 * Created by SharpDevelop.
 * User: issuser
 * Date: 2011-1-4
 * Time: 15:54
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Data;
using System.Linq;
using NLite.Data.SqlTypes;
using System.Reflection;
using NLite.Reflection;
using System.Collections.Generic;
using System.Data.Common;
using System.Collections;
using NLite.Data.Driver.Internal;
using NLite.Data.Schema.Provider.Internal;

namespace NLite.Data.Driver
{
	/// <summary>
	/// Description of OracleDataClientDriver.
	/// </summary>
	public class OracleDataClientDriver:DriverBase
	{
        
        private IDictionary<DbTypeCode, object> TypeTable;

		public OracleDataClientDriver()
		{
            SchemaProvider = new OracleSchemaProvider();

            TypeTable = new Dictionary<DbTypeCode, object>();

            TypeTable[DbTypeCode.Boolean] = OracleClientFactory.OracleDbType.GetField("Int16");
            TypeTable[DbTypeCode.UInt16] = TypeTable[DbTypeCode.Boolean];
            TypeTable[DbTypeCode.UInt32] = OracleClientFactory.OracleDbType.GetField("Int32");
            TypeTable[DbTypeCode.UInt64] = OracleClientFactory.OracleDbType.GetField("Int64");


            TypeTable[DbTypeCode.Clob] = OracleClientFactory.OracleDbType.GetField("Clob");
            TypeTable[DbTypeCode.NClob] = OracleClientFactory.OracleDbType.GetField("NClob");

            TypeTable[DbTypeCode.Guid] = OracleClientFactory.OracleDbType.GetField("Raw");
		}

        public override bool UseNamedPrefixInSql
        {
            get { return true; }
        }

        public override bool UseNamedPrefixInParameter
        {
            get { return true; }
        }

        public override string NamedPrefix
        {
            get { return ":"; }
        }

        protected override void InitializeParameter(System.Data.IDbDataParameter cp, IDataParameter p)
        {

            switch (p.Identity.Type.DbType)
            {
               
                case DbTypeCode.Guid:
                     cp.Size = 16;
                    break;
                default:
                    if (TypeTable.ContainsKey(p.Identity.Type.DbType))
                        OracleClientFactory.SetOracleDbTypeProperty(cp, TypeTable[p.Identity.Type.DbType]);
                    else
                        cp.DbType = (DbType)p.Identity.Type.DbType;
                    break;
            }

            cp.Value = p.Value;
        }

        protected override System.Data.Common.DbConnection InternalCreateConnection()
        {
            return OracleClientFactory.Instance.CreateConnection();
        }

       

        
	}
}
